const {
  handleRequest,
  handleError,
  handleSuccess
  // handleThrottle
} = require('../utils/handle')

const fileCtrl = { upload: {} }

fileCtrl.upload.POST = async (ctx) => {
  // let files = ctx.request.body.files
  // if (!files) {
  //   handleError({
  //     ctx,
  //     message: '请上传文件'
  //   })
  // }
  const fs = require('fs')
  const file = ctx.request.body.files.file // 获取上传文件
  const reader = fs.createReadStream(file.path) // 创建可读流
  const ext = file.name.split('.').pop() // 获取上传文件扩展名
  const upStream = fs.createWriteStream(`upload/${Math.random().toString()}.${ext}`) // 创建可写流
  reader.pipe(upStream) // 可读流通过管道写入可写流
  // const path = require('path')
  // const uploadFile = require('../utils/fileUtils').uploadFile
  // const result = await uploadFile(ctx, {
  //   dir: 'album',
  //   path: path.resolve(__dirname, '../../upload')
  // })
  // handleSuccess({
  //   ctx,
  //   result
  // })
  // const fs = require('fs')
  // const path = require('path')
  // let file = files.file
  // let tmpPath = file.path
  // let uploadPath = path.resolve(__dirname, '../../upload/')
  // let targetPath = uploadPath + '/' + file.name
  // await fs.rename(tmpPath, targetPath, (err) => {
  //   if (err) throw err
  // })
}

export default {
  upload: ctx => handleRequest({ctx, controller: fileCtrl.upload})
}
